
#include "i2c.h"
#include "main.h"
//#include "LPC214x.h"


unsigned I2CAddress;
unsigned I2C_reg_Address;
unsigned I2CData;
#define read 1
#define write 0
char i2cact=0;
char write_count=0;
char read_count=6;
char multi_read=0;
char acc_data[7];
float acc_x=0;
float acc_y=0;
float acc_z=0;


char acc_test(void)
{
return acc_data[1];
}
float acc_cal_x(void)
  {
    acc_x=(  acc_data[0]&0b00001111+acc_data[1]*16 )/4096.0 *4.0-2;
    return acc_x;
  }
float acc_cal_y(void)
  {
    acc_y=(  acc_data[2]&0b00001111+acc_data[3]*16 )/4096.0 *4.0-2;
    return acc_y;
  }
float acc_cal_z(void)
  {
    acc_z=(  acc_data[4]&0b00001111+acc_data[5]*16 )/4096.0 *4.0-2;
    return acc_z;
  }

void i2c_init(void)
{
  PINSEL0|=PINSEL0_SCL0|PINSEL0_SDA0;
  I20SCLH = 400;
  I20SCLL = 400;     //PCLK=12MX6    FRQ=PCLK/(100+100)=360KHZ
 // ISR SETTINGS
   VICIntSelect &= ~( 1<<9 );  //I2C0 
   VICVectCntl3 = 0x20 | 9;    //I2C0
   VICVectAddr3 = (unsigned int)I2CISR;
   VICIntEnable |= ( 1<<9 );   // ISR ENABLE
   I20ADR=0X72;


}

void i2c_acc_init(void)
{
//  I2CAddress = gyro_add;
  I2CAddress = acc_add;
  I2CData = acc_init;
  I2C_reg_Address=CTRL_REG1;
  write_count=1;
  i2cact=write;
  multi_read=0;
  I20CONCLR = 0x000000FF;
  I20CONSET = 0x00000040;
  I20CONSET = 0x00000020; //start


}
void get_acc_x(void)
{
  I2CAddress = acc_add;
  I2C_reg_Address = OUT_X_LSB;
  i2cact=write;
  multi_read=1;
  read_count=0;
  I20CONSET = 0x10;  //stop will transmitted
  I20CONCLR = 0x000000FF;
  I20CONSET = 0x00000040;   //enable
  I20CONSET = 0x00000020;   //start
}
float get_acc_y(void)
{}
float get_acc_z(void)
{}

void I2CISR (void)
{
  
  switch (I20STAT)
  {
    case (0x08):   //start transmitted
    I20DAT=0b00111000;
//      if (i2cact)     //read
//        {
//          I20DAT=I2CAddress|1; 
//        }
//        else    //write
//        {
//          I20DAT=I2CAddress; 
//        }
      I20CONCLR=0x20;  // clear sta
    break;

    case (0x10):  // repeat start transmitted
      I20CONCLR=0x20;  // clear start
      I20DAT=I20DAT=I2CAddress|1; ;  //read
    break;

    case (0x18):
      acc_data[1]=0x23;
      if (i2cact)     //read  write reg_address first
        {
          I20DAT=I2C_reg_Address; 
        }
        else    //write reg_add
        {
          I20DAT=I2C_reg_Address; 
        }
      
    break;
    
    case (0x20):
    I20DAT = I2CAddress;
    break;
    
    case (0x28):
      if(multi_read==0)   //not multi read
      {
        if (write_count>0)
        {
        I20DAT=I2CData;    //write acc_init to acc sensor
        write_count--;
        }
        else
        {
        I20CONSET = 0x10;  //stop will transmitted
        }
      }
      else
      {
        I20CONSET = 0x00000020; // repeat start
      
      }
    break;
    

    
    case (0x30):
    break;
    
    case (0x38):
    break;
    
    case (0x40):
     I20CONSET = 1<<2; // AA=1 ACK will be returned

    break;
    
    case (0x48):
    break;
    
    case (0x50):
      if(read_count<6)
      {
        acc_data[read_count]=I20DAT;
        if(read_count==4)
        {
          I20CONCLR= 1<<2; // AA=0 
        }
        read_count++;
      }
      else
      {
       
      }
    break;
    
    case (0x58):
         acc_data[read_count]=I20DAT;
         acc_data[1]=0x23;
         I20CONSET = 0x10;  //stop will transmitted
        


    
    break;
    
    
    default:
    acc_data[1]=0x23;
    break;
  }
}
